package com.cl.code.common.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class GenericUtils {

    public static Class<?> getGenericType(Class<?> clazz, int index) {
        // 获取当前类的泛型类型
        Type type = clazz.getGenericSuperclass();
        if (type instanceof ParameterizedType parameterizedType) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            if (actualTypeArguments.length > 0) {
                return (Class<?>) actualTypeArguments[index];
            }
        }
        throw new IllegalArgumentException("cloud not find generic type for class: " + clazz.getName());
    }

}
